<template>
	<view class=" flex flex_column login">
		<image src="../../static/log0.png" mode="" class="logo"></image>
		<p class="title">登录</p>
		<view class="padding26">
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm-login">
				<el-form-item label="" prop="phone" class="el-form-item-login">
					<el-input prefix-icon="el-icon-user-solid" v-model="ruleForm.phone" class="el-input-login"
						placeholder="请输入手机号"></el-input>
				</el-form-item>
				<el-form-item label="" prop="password" class="el-form-item-login">
					<el-input prefix-icon="el-icon-s-goods" v-model="ruleForm.password" class="el-input-login"
						placeholder="请输入密码" show-password></el-input>
				</el-form-item>
			</el-form>
			<view class="wangji">
				<text @click="rember()">忘记密码？</text>
			</view>
			<view class="login-btn">
				<button class="btn-size btn-b" @click="userLogin('ruleForm')">登录</button>
				<!-- <button class="btn-size btn-w" @click="userLogin(1)">个体用户登录</button> -->
			</view>
			<view class="login-footer">
				<p class="login-btn-p" @click="userLogins()"><text>还没有账号？</text>去注册</p>
				<view class="flex" style="justify-content: center;">
					<el-checkbox v-model="checked">
										 <p class="login-footer-p" @click="goShow()">登录代表同意 <text>《服务协议》</text> 及 <text>《隐私协议》</text> </p>
					</el-checkbox>
				</view>
				<!-- <p class="login-footer-p">登录前请阅读</p> -->
				<!-- <p class="login-footer-p">桂ICP备2023009823号</p> -->
			</view>
		</view>

	</view>
</template>

<script>
	import {
		getLogin,
		getInfo,

	} from "@/api/login"
	import {
		dictFunAll,
		dictSect
	} from "@/util/dictionary.js"
	export default {
		data() {
			var checkPhone = (rule, value, callback) => {
				if (!value) {
					return callback(new Error("手机号不能为空"));
				} else {
					const reg = /^1[3-9][0-9]\d{8}$/;
					// 这里定义的reg变量没有加引号，所以是一种特殊的数据类型，即正则，可以直接用test()方法
					if (reg.test(value)) {
						callback();
					} else {
						// 具体错误信息没有提示
						return callback(new Error("请输入正确的手机号"));
					}
				}
			};
			return {
				checked:false,
				ruleForm: {
					phone: "",
					password: "",
					code: "",

				},
				rules: {
					phone: [{
						required: true,
						message: '请输入手机号',
						trigger: 'blur'
					}, {
						validator: checkPhone,
						trigger: "blur"
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}]
				}
			};
		},
		methods: {
			rember() {
				uni.navigateTo({
					url: "/pages/login/resect/resect"
				})
			},
			userLogin(formName) {
				this.$refs[formName].validate((valid) => {
					if(!this.checked){
						this.$message({
							message: `请先勾选同意服务协议和隐私协议`,
							type: 'error',
							center: true,
							offset: 0.1
						});
						return
					}
					if (valid) {
						let obj = {
							"clientId": "eeb9377d4b6debbc0a6e8756bbbbb4c7",
							"grantType": "phonePassword",
							"tenantId": "000001",
							"password": this.ruleForm.password,
							"phonenumber": this.ruleForm.phone,
						}
						uni.removeStorageSync('userInfoData_') //移除登录信息
						getLogin(obj).then((res) => {
							console.log(res);
							if (res.code == 200) {
								uni.setStorageSync("userInfoData_", res.data)
								this.$message({
									message: `登陆成功`,
									type: 'success',
									center: true,
									offset: 0.1
								});
								this.getInfo_()
								 let currentUrl = uni.getStorageSync("currentUrl");
								 console.log(currentUrl);
								 if(currentUrl){
									 console.log(currentUrl);
									 setTimeout(() => {
									 	uni.navigateTo({
									 		url: "/pages/index/position/position?data=" + JSON.stringify(currentUrl)
									 	})
										uni.removeStorageSync('currentUrl')
									 }, 500)
									 
								 }else{
									 setTimeout(() => {
									 	uni.switchTab({
									 		url: "/pages/index/index"
									 	})
									 }, 500)
								 }
								
							} else {
								this.$message({
									message: res.msg,
									type: 'error',
									center: true,
									offset: 0.1
								});
							}
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			getInfo_() {
				getInfo().then(res => {
					console.log("个人信息", res);
					dictFunAll()
					uni.setStorageSync("my_userInfo", res.data)
				})
			},
			userLogins() {
				uni.navigateTo({
					url: "/pages/login/userlogin/userlogin",
				})
			},
			goShow(){
				uni.navigateTo({
					url: "/pages/login/service/service",
				})
			}


		}
	}
</script>
<style lang="scss">
	::v-deep .el-form-item__error {
		position: absolute;
		top: 50px;
	}
</style>
<style lang="scss" scoped>
	.login {

		// width: 100%;
		// height: 100%;
		.login-btn {
			margin-top: 56px;
			width: 100%;
		}

		.logo {
			// margin-top: 44px;
			margin-bottom: 24px;
			width: 100%;
			height: 30vh;
			border-radius: 12px 12px 12px 12px;
		}

		.title {
			margin-bottom: 24px;
			text-align: center;
			font-size: 24px;
			font-family: 'Source Han Sans CN-Regular, Source Han Sans CN';
			font-weight: 400;
			color: #3D3D3D;
		}

		.btn-size {
			width: 100%;
			height: 48px;
			margin-bottom: 22px;
		}

		.login-footer {
			// position: absolute;
			// bottom: 46px;

			.login-footer-p {
				font-size: 11px;
				font-family: 'PingFangSC-Regular-Regular, PingFangSC-Regular';
				font-weight: 400;
				color: #646466;
				text-align: center;

				text {
					color: #1C53D9;
				}
			}
		}

		.wangji {
			width: 100%;
			display: flex;
			justify-content: flex-end;

			text {
				font-size: 12px;
				font-family: 'Source Han Sans CN-Regular, Source Han Sans CN';
				font-weight: 400;
				color: #2F6CBC;
			}
		}

		.login-btn-p {
			margin-bottom: 30px;

			text {
				color: #8B8B8B;
			}

			text-align: center;
			font-size: 14px;
			font-family: 'HarmonyOS Sans SC-Regular, HarmonyOS Sans SC';
			font-weight: 400;
			color: #2F6CBC;
		}
	}
</style>
